package org.example.producer;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

import java.util.UUID;

@Slf4j
public class SimpleProducer {
    @SneakyThrows
    public static void createProducer() {
        // 消息生产者组
        DefaultMQProducer niweiProducerGroup = new DefaultMQProducer("niwei_producer_group");
        niweiProducerGroup.setNamesrvAddr("192.168.31.104:9876");
        niweiProducerGroup.start();

        for (int i = 0; i < 100; i++) {
            Message msg = new Message("topic", "tag", UUID.randomUUID().toString().getBytes(RemotingHelper.DEFAULT_CHARSET));
            // 同步发送
            SendResult send = niweiProducerGroup.send(msg);
            log.info("发送结果 SendResult：{}", send);
        }
        // 安全退出当前生产组
        niweiProducerGroup.shutdown();
    }
}
